﻿using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.Library.CommonClass
{
   public class RecentlyUseRecord
    {
        private static BarSubItem _CurrentList = null;

        public static void LoadRecentlyRecordList(BarSubItem owner, ToolStrip menus)
        {
            string path = GetIniFile();
            Lamex.Common.IniFile ini = new Common.IniFile(path);
            List<string> lst = ini.ReadKeyList("FrequentlyFunction");
            List<RecordList> recLst = new List<RecordList>();
            
            foreach(string s in lst)
            {
                RecordList model = new RecordList();
                if (!string.IsNullOrEmpty(ini.IniReadValue("FrequentlyFunction", s)))
                {
                    string date = ini.IniReadValue("FrequentlyFunction", s);

                    model.Name = s;
                    if (!string.IsNullOrEmpty(date))
                        model.DateSort = Convert.ToDateTime(date);
                    recLst.Add(model);
                }
                
            }
            List<RecordList> dicList = recLst.OrderByDescending(x => x.DateSort).ToList().Take(10).ToList();
          

            _CurrentList = owner;
            foreach (RecordList model in dicList)
            {

                foreach (ToolStripMenuItem moduleTopMenu in menus.Items)
                {
                    //递归加载
                    EnumEachItem(moduleTopMenu,  owner,model.Name);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="item"></param>
        private static void EnumEachItem(ToolStripMenuItem item, BarSubItem owner,string functionId)
        {

            for (int i = 0; i < item.DropDownItems.Count; i++)
            {
                ToolStripMenuItem dropItem = item.DropDownItems[i] as ToolStripMenuItem;
                ToolStripItem dropItema = item.DropDownItems[i] as ToolStripItem;
                
                BarCheckItem owneritem = new BarCheckItem(owner.Manager);
                owneritem.Caption = dropItema.Text;
                owneritem.Tag = dropItema;
                owneritem.Name = dropItema.Name;
                owneritem.ItemClick += new ItemClickEventHandler(menuOwner_ItemClick);
                if (functionId == dropItema.Name)
                    owner.ItemLinks.Add(owneritem);
                EnumEachItem(dropItem, owner, functionId);
            }
        }
        static void menuOwner_ItemClick(object sender, ItemClickEventArgs e)
        {
            if ((e.Item.Tag != null) && (e.Item.Tag is ToolStripItem))
                (e.Item.Tag as ToolStripItem).PerformClick();
            //if (_CurrentList != null)
            //{
            //    foreach (BarItemLink item in _CurrentList.ItemLinks)
            //    {
            //        if (item.Item is BarCheckItem)
            //        {
            //            (item.Item as BarCheckItem).Checked = false;
            //        }

            //    }

            //    (e.Item as BarCheckItem).Checked = true;
                
            //}
        }
        /// <summary> 
        /// 验证文件是否存在 
        /// </summary> 
        /// <returns>布尔值</returns> 
        public static string GetIniFile()
        {
            string inipath = Application.StartupPath + @"\config\FrequentlyFunction.ini";
            if (!File.Exists(inipath))
            {
                File.Create(inipath);
            }
            return inipath;
        }
    }
}
